import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Link } from 'react-router';
import Util from "../../utils/Util";
import { Tool } from "../../utils/Tool";
import { setCommonSystemLoading } from "../../action/Action";
import * as DataName from "../../action/DataNameConfig";
import { GetUserList } from "../../action/ActionUser";
import { Card, Table, Switch } from "antd";
class Main extends Component {
    constructor(props){
        super(props);
        this.state = {
            ...DataName.USER_LIST_FILTER
        }
        this.handleTableChange=(pagination, filters, sorter)=>{
            this.setState({
                pageNo: pagination.current-1,
                pageSize: pagination.pageSize
            },()=>{
                this.getData();
            })
        }
        this.getData=(flag)=>{
            let { dispatch } = this.props;
            console.log(this.state);
            if(flag){
                dispatch(setCommonSystemLoading(DataName.BASE_CARD_LOADING,true));
            }
            dispatch(setCommonSystemLoading(DataName.BASE_TABLE_LOADING,true));
            dispatch(GetUserList({...this.state}));
        }
    }
    componentWillMount(){
        Util.setMenuOpenKey(this.props,"user");
        this.getData(true);
    }
    render() {
        let { BaseData, CommonSystem } = this.props;
        let userList = BaseData[DataName.USER_LIST] || {};
        const pagination = Util.paginationInit(userList);
        const columns = [
            {
                title: '姓名',
                render: (text, record, index) => (
                    <Link to={"/app/user/info/"+record.uid}>{record.name}</Link>
                ),
                key: 'uid'
            },
            {
                title: 'level',
                dataIndex: 'level',
                render: (text, record) => (
                    <span>{record.level}</span>
                )
            },
            {
                title: '是否为代理',
                render: (text, record) => (
                    <Switch checkedChildren="是" unCheckedChildren="否" checked={record.level>0} disabled />
                )
            },
            {
                title: '邀请码',
                dataIndex: 'invite_code'
            },
            {
                title: '创建时间',
                dataIndex: 'create_time',
                render: (text, record) => (
                    <span>{Tool.formatDateTime(record.create_time,1)}</span>
                )
            },
            {
                title: '操作',
                dataIndex: 'type',
                render: (text, record) => (
                    <span>{record.type === 0? "逾期":"异常"}</span>
                )
            }
        ];

        return (
            <div className={"page-common"}>
                <Card title={"用户管理"} bordered={false} loading={CommonSystem[DataName.BASE_CARD_LOADING] || false}>
                    <Table rowkey={"uid"} columns={columns} dataSource={userList.content?userList.content:[]} onChange={this.handleTableChange} bordered={false} loading={CommonSystem[DataName.BASE_TABLE_LOADING] || false} className={"table-center"} pagination={pagination} />
                </Card>
            </div>
        )
    }
}

export default connect(state=>({
    User: state.User,
    CommonSystem: state.CommonSystem,
    BaseData: state.BaseData
}))(Main);
